package com.huidao.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.huidao.entity.User;
import com.huidao.service.UserService;
import com.huidao.service.UserServiceImpl;

/**
 * Servlet impl123123ementation class LoginServlet
 */
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		User user=new User();
		user.setUserName(request.getParameter("username"));
		user.setUserPassword(request.getParameter("password"));
		UserService s=new UserServiceImpl();
		int data=s.Login(user);
		if(data==1){
			request.getSession().setAttribute("user", user);
			if(user.getRole().getRoleId()==2){
				response.sendRedirect("buyer/BuyerGoodsListServlet");
			}else if(user.getRole().getRoleId()==3){
				response.sendRedirect("seller/SellerGoodsListServlet");
			}else{
				response.sendRedirect("Administrators.jsp");
			}
		}else if(data==0){
			response.getWriter().print("您输入的用户名不存在");
			
		}else if(data==2){
			response.getWriter().print("您输入的密码错误");
			
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
